package mge.ui 
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.events.TimerEvent;
  import flash.geom.Point;
  import flash.text.AntiAliasType;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.text.TextFormat;
  import flash.text.TextFormatAlign;
  import flash.utils.Timer;
	
	/**
   * ...
   * @author ivan
   */
  public class ToolTipBox extends Sprite 
  {
    
    private static const COLOR:uint = 0xFFFFFF;
    private static const ALPHA:uint = 0.7;
    
    /**
     * 
     * @param	target
     * @param	text
     * @param	_textField
     */
    public function ToolTipBox(target:Sprite, text:String, _textField:TextField = null)
    {
      super();
			this.target = target;
      this.text = text;
      this.textField = _textField;
      this.targetAlign = targetAlign;
      
      this.mouseEnabled = false;
      this.mouseChildren = false;
      
      if (!textField)
      {
        textField = new TextField();
        textField.autoSize = TextFieldAutoSize.CENTER;
        var format:TextFormat = new TextFormat("Helvetica", 10, 0x333333);
        format.align = TextFormatAlign.CENTER;
        textField.defaultTextFormat = format;
        textField.multiline = true;
        textField.wordWrap = true;
        textField.antiAliasType = AntiAliasType.ADVANCED;
        
        // textField.background = true; // debug only
      }
      
      //textField.text = text;
      textField.htmlText = text;
      
      addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }
    
    private function onAddedToStage(e:Event):void
    {
      removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
      addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
      target.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
      target.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
    }
    
    private function onRemovedFromStage(e:Event):void
    {
      removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
      addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
      target.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
      target.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
    }
    
    private function onRollOver(e:Event):void
    {        
      timer.reset();
      timer.start();
      timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
    }
    
    private function onRollOut(e:Event):void
    {
      timer.stop();
      timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
      
      // Clears stuff
      if (drawed)
      {
        graphics.clear();
        removeChild(textField);
        drawed = false;
      }
    }
    
    private function onTimerComplete(e:Event):void
    {
      timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
      draw();
    }
    
    private function draw():void
    {
      drawed = true;
      
      var w:Number = textField.width + margin;
      var h:Number = textField.height + margin;
      
      var ellipse:Number = 20;
      
      // shadow      
      graphics.beginFill(0x000000, 0.25);
      graphics.drawRoundRect(3, 3, w, h, ellipse);
      graphics.endFill();
      
      // border
      graphics.beginFill(0x000000, 1);
      graphics.drawRoundRect( -1, -1, w + 2, h + 2, ellipse);
      graphics.endFill();
      
      // content
      graphics.beginFill(0xFFFFFF, 1);
      graphics.drawRoundRect(0, 0, w, h, ellipse);
      graphics.endFill();
      
      var bp:Point = findBestPosition(w, h);
      var p:Point = bp.clone();
      
      x = p.x;
      y = p.y;
      
      // Situates textfield centered in sprite.
      textField.x = margin / 2;
      textField.y = margin / 2;
      
      addChild(textField);
    }
    
    private function findBestPosition(my_w:Number, my_h:Number):Point
    {
      // It tries to adjust to the mouse as close as possible.
      var p:Point = new Point(stage.mouseX, stage.mouseY + 24);
      
      // If it's behind bottom
      if (p.y + my_h > stage.stageHeight)
        p.y -= (24 + my_h + 1);
      
      // if it's beyond right
      if (p.x + my_w > stage.stageWidth)
        p.x -= (my_w);
        
      return p;
    }
    
    private const margin:Number = 10;
    
    private var target:Sprite;
    private var timer:Timer = new Timer(500, 1);
    private var textField:TextField;
    private var text:String;
    private var drawed:Boolean;
    private var targetAlign:String;
    
  }

}